package com.bw.mallshop.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.dao.UserSingle;
import com.bw.mallshop.entity.AddressEntity;
import com.bw.mallshop.entity.ModifyAddressEntity;
import com.bw.mallshop.entity.ShipAddress;
import com.bw.mallshop.field.EventAction;
import com.bw.mallshop.mvvm.model.ShipAddressModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShipAddressViewModel extends BaseViewModel<ShipAddressModel> {

    public ObservableField<String>name=new ObservableField<>();
    public ObservableField<String>phone=new ObservableField<>();
    public ObservableField<String>address = new ObservableField<>();
    public ShipAddress shipAddress;

    public void save(){
        if (shipAddress!=null){
            shipAddress.ship_user_name = name.get();
            shipAddress.ship_user_mobile = phone.get();
            shipAddress.ship_address = address.get();
            //修改操作
            Map<String,Object>map=new HashMap<>();
            map.put("id",shipAddress.id);
            map.put("shipAddress",shipAddress.ship_address);
            map.put("shipIsDefault",0);
            map.put("shipUserMobile",shipAddress.ship_user_mobile);
            map.put("shipUserName",shipAddress.ship_user_name);
            map.put("userId",shipAddress.user_id);
            m.requestUpdateAddress(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            //添加操作
            shipAddress = new ShipAddress();
            shipAddress.ship_user_name = name.get();
            shipAddress.ship_user_mobile = phone.get();
            shipAddress.ship_address = address.get();
            //修改操作
            Map<String,Object>map=new HashMap<>();
            map.put("shipAddress",shipAddress.ship_address);
            map.put("shipIsDefault",0);
            map.put("shipUserMobile",shipAddress.ship_user_mobile);
            map.put("shipUserName",shipAddress.ship_user_name);
            map.put("userId", UserSingle.getInstance().userEntityValues.id);
            m.requestAddAddress(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }

    public void finish(){
        finishPage();
    }

    @Override
    protected void initData() {
        if (shipAddress!=null){
            name.set(shipAddress.ship_user_name);
            phone.set(shipAddress.ship_user_mobile);
            address.set(shipAddress.ship_address);
        }

    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof AddressEntity){
            if (entity.message.equals("添加地址成功")){
                finishPage();
                //刷新列表
//                EventAction action = new EventAction();
//                action.action="ship_refresh";
//                EventBus.getDefault().post(action);
            }
        }else if (entity instanceof ModifyAddressEntity){
            if (entity.message.equals("修改成功")){
                finishPage();
                //刷新列表
//                EventAction action = new EventAction();
//                action.action="ship_refresh";
//                EventBus.getDefault().post(action);
            }
        }
    }
}
